﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TestLink2Net.Dal.Interfaces;
using TestLink2Net.Entities;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace TestLink2Net.Dal
{
    public class SecurityDB : ISecurityDB
    {
        private SecurityDB()
        {
        }

        public static ISecurityDB Instance
        {
            get
            {
                return new SecurityDB();
            }
        }

        #region ISecurityDB Members

        public User GetUser(string UserName)
        {
            User user = null;
            try
            {
                Database database = DataAccess.DefaultDatabase;

                using (DbCommand command = database.GetSqlStringCommand(Util.SelectUser))
                {
                    database.AddInParameter(
                           command, "@LogIn", DbType.String, UserName);
                    using (IDataReader reader = database.ExecuteReader(command))
                    {
                        if (reader.Read())
                        {
                            user = new User()
                            {
                                ID = int.Parse(reader[0].ToString()),
                                UserName = reader[1].ToString(),
                                Password = reader[2].ToString()
                            };
                        }
                    }
                }

            }
            catch (Exception)
            {
            }
            return user;
        }

        #endregion
    }
}
